package ir;

import java.util.Set;

/**
 * generates all Soap2Gwt and Gwt2Soap methods to
 * map each datatype to its corresponding type
 * @author tchau@google.com (Your Name Here)
 *
 */
public class TranslatorGenerator implements Generator {

  private Set<DataNode> dataTypes;
  
  public TranslatorGenerator(Set<DataNode> dnodes) {
    dataTypes = dnodes;
  }
  
  public String gen() {
    String translatorClass = "";
    
    
    
    // generate a pair of translator methods for each data node
    for (DataNode n : dataTypes) {
      
        String gwt2soap = n.generateGwt2Soap();
        String soap2gwt = n.generateSoap2Gwt();
        String array_soap2gwt = n.generateArraySoap2Gwt();
        String array_gwt2soap = n.generateArrayGwt2Soap();
        
        translatorClass += gwt2soap + soap2gwt + array_soap2gwt + array_gwt2soap;
    }
        
    
    translatorClass = "\npublic class Translator { \n\n" +
      INDENT + "private static Translator instance = new Translator();\n" +
      INDENT + "public Translator() {}\n" +
      INDENT + "public static Translator instance() { return instance; }\n\n" +
      translatorClass + " }";
    
    String imports = getImports();
    translatorClass = imports + translatorClass;
    
    String package_declare = "package com.google.server.translator;\n\n";
    translatorClass = package_declare + translatorClass;
    
    return translatorClass;
  }

  private String getImports() {
    
    String imports = "import java.util.*;\n";
    for (DataNode d : dataTypes) {
      imports += "import " + d.underlier.getCanonicalName() + ";\n";
      imports += "import " + d.getPackage() + "." + d.getGeneratedClassName() + ";\n";
    }
    
    
    return imports;
  }
  
  
  /* E.g.:
   * 
   * GwtAdData Soap2Gwt (AdData in) {
   *    GwtAdData out = new GwtAdData();
   *    
   *    // Template: out.set<FIELD>(in.get<FIELD>);
   *    out.setName(in.getName());
   *    out.setID(in.getID());
   *    
   *    ... etc ...
   *    
   *    return out;
   * }
   * 
   */
  
  // we need to generate this method for each data type as well
  // as generate the symmetrical Gwt2Soap method.
  

}
